﻿using System;

namespace InfoJetSoft.Service.Util
{
	
	public class FieldUtil
	{
		
		public static int[] nextCoords(int[] coords, int index)
		{
			int[] nexts = new int[coords.Length + 1];
			for (int i = 0; i < coords.Length; i++)
			{
				nexts[i] = coords[i];
			}
			nexts[nexts.Length - 1] = index;
			return nexts;
		}

		public static string getRowId( string fieldId ){
			int index = fieldId.IndexOf( JetConstant.INNER_SEPARATOR );
			if( index < 0 ){
				index = fieldId.Length;
			}
			string rowId = fieldId.Substring( 0, index );
			index = rowId.LastIndexOf( "_" );
			if( index < 0 ){
				index = 0;
			}
		
			rowId = rowId.Substring( index + 1 );
			return rowId;
		}
	
		public static bool isNotCousin( String id1, String id2 ){
			string parentCoord1 = getParentCoord( id1 );
			string parentCoord2 = getParentCoord( id2 );
			if( parentCoord1 != null && parentCoord2 != null ){
				if( parentCoord1.Equals( parentCoord2 ) ){
					//brother
					return true;
				}else{
					string ancestorCoord1 = getParentCoord( parentCoord1 );
					string ancestorCoord2 = getParentCoord( parentCoord2 );
					if( ancestorCoord1 != null && ancestorCoord2 != null && ancestorCoord1.Equals( ancestorCoord2 ) ){
						//cousin
						return false;
					}else{
						return true;
					}
				}
			}
			return false;
		}
	
		private static string getParentCoord( string id ){
			int index1 = id.LastIndexOf( "_" );
			if( index1 <= 0 ){
				return null;
			}
			int index2 = id.LastIndexOf( "_", index1 - 1 );
			if( index2 > 0 ){
				return id.Substring( 0, index2 );
			}
			return null;
		}

		public static string getJetIdNodeName( string jetId ){
			//CID 0317 //2009.06.12 //Begin
			jetId = FieldUtil.getPureJetId(jetId);
			//CID 0317 //2009.06.12 //End
			int atIndex = jetId.IndexOf( "@" );
			if( atIndex < 0 ){
				char[] chars = jetId.ToCharArray();
				bool beforeIsUnderline = false;
				int i = 0;
				for( ; i < chars.Length; i ++ ){
					char c = chars[ i ];
					if( c == '_' ){
						if( beforeIsUnderline ){
							break;
						}else{
							beforeIsUnderline = true;
						}
					}else{
						if( Char.IsDigit( c ) ){
							beforeIsUnderline = false;
						}else{
							break;
						}
					}
				}
				return jetId.Substring( i );
			}else{
				string attrName = jetId.Substring( atIndex );
				return attrName;
			}
		}

		public static string[] parseJetId( string jetId ){
			string elementId = jetId;
			string attributeName = "";
			int commaIndex = elementId.IndexOf( JetConstant.INNER_SEPARATOR );
			if (commaIndex > 0) {
				elementId = elementId.Substring(0, (commaIndex) - (0));
			}
			if (elementId.IndexOf("@") > 0) {
				StringTokenizer atTokenizer = new StringTokenizer(elementId, "@");
				elementId = atTokenizer.NextToken();
				attributeName = atTokenizer.NextToken();
			}
			return new string[]{ elementId, attributeName };
		}

		public static String getPureJetId( String htmlId ){
			int semiIndex = htmlId.IndexOf( JetConstant.INNER_SEPARATOR );
			if (semiIndex > 0) {
				return htmlId.Substring( 0, semiIndex );
			}else{
				return htmlId;
			}
		}

	}
}